<template>
  <div>
    <el-card class="box-card">
      <el-row :gutter="8"
              type="flex"
              align="middle"
              style="margin-bottom:10px">

        <el-col :span="1.5">
          <el-button size="mini"
                     type="primary"
                     icon="el-icon-plus"
                     @click="add">新增</el-button>
        </el-col>

      </el-row>
      <el-table :data="tableData"
                style="width: 100%"
                :header-cell-style="{background:'#f8f8f9','text-align':'center'}"
                @selection-change="handleSelectionChange"
                :cell-style="{'text-align':'center'}">
        <el-table-column type="selection"
                         width="120">
        </el-table-column>
        <el-table-column prop="configId"
                         label="編號"
                         width="120">
        </el-table-column>
        <el-table-column prop="configValue"
                         label="名称">
        </el-table-column>
        <el-table-column prop="sort"
                         label="排序"
                         width="120">
        </el-table-column>

        <el-table-column prop="mk"
                         label="备注">

        </el-table-column>

        <el-table-column label="操作">
          <template v-slot="data">
            <el-button icon="el-icon-edit"
                       type="text"
                       @click="updateHandler(data.row.configId)">修改</el-button>
            <el-button icon="el-icon-delete"
                       type="text"
                       @click="dele(data.row.configId)">删除</el-button>
          </template>

        </el-table-column>

      </el-table>
      <div class="page"
           v-show="total!=0">
        <el-pagination background
                       @size-change="handleSizeChange"
                       @current-change="handleCurrentChange"
                       :current-page="queryParams.pageNum"
                       :page-sizes="[3, 5, 7, 9]"
                       :page-size="queryParams.pageSize"
                       layout="total, sizes, prev, pager, next, jumper"
                       :total="total">
        </el-pagination>
      </div>
    </el-card>

    <div>
      <el-dialog :title="flag?'添加配置':'修改配置'"
                 :visible.sync="dialogVisible"
                 width="40%">
        <el-form :model="formData"
                 ref="ruleForm"
                 label-width="80px">

          <el-form-item label="名称">
            <el-input size="small"
                      v-model="formData.configValue"
                      placeholder="名称"></el-input>
          </el-form-item>

          <el-form-item label="排序">
            <el-input size="small"
                      v-model="formData.sort"
                      placeholder="排序"></el-input>
          </el-form-item>
          <el-form-item label="备注">
            <el-input size="small"
                      v-model="formData.mk"
                      placeholder="备注"></el-input>
          </el-form-item>

        </el-form>
        <span slot="footer"
              class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary"
                     @click="update">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryParams: { pageNum: 1, pageSize: 3 },
      tableData: [],
      total: 0,
      loading: false,
      dialogVisible: false,
      checkItem: [],
      flag: true,
      formData: {
        configId: null,
        configCode: null,
        configValue: null,
        sort: null,
        configType: null,
        mk: null
      }
    }
  },
  methods: {
    handleSelectionChange(val) {
      this.checkItem = val
    },
    handleCurrentChange(val) {
      this.queryParams.pageNum = val
      this.getData()
    },
    handleSizeChange(val) {
      this.queryParams.pageSize = val
      this.getData()
    },
    async getData() {
      this.loading = true
      let { data } = await this.$axios.get('com/appConfig/hobbyList', { params: this.queryParams })
      console.log(data)
      this.tableData = data.rows
      this.loading = false
      this.total = data.total
    },

    dele(id) {
      this.deleteHandler(id)
    },
    delMore1() {
      let ids = this.checkItem.map((el) => el.configId)
      this.deleteHandler(ids)
    },
    deleteHandler(id) {
      this.$confirm(`是否确认删除意见编号为${id.toString()}的数据项?`, '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.del(id)
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
    },
    async del(id) {
      let { data } = await this.$axios.delete(`com/appConfig/${id}`)
      if (data.code != 200) return this.$message.error(data.msg)
      this.$message({
        type: 'success',
        message: data.msg
      })
      this.getData()
    },
    add() {
      this.flag = true
      this.formData = {
        configId: null,
        configCode: 'Xqah',
        configValue: null,
        sort: null,
        configType: null,
        mk: null
      }
      this.dialogVisible = true
    },
    async updateHandler(id) {
      this.flag = false
      this.dialogVisible = true
      let { data } = await this.$axios.get(`com/appConfig/${id}`)
      console.log(data)
      this.formData = data.data
    },
    async update() {
      let data = null
      this.dialogVisible = false
      if (this.formData.configId) {
        data = await this.$axios.put('com/appConfig', this.formData)
      } else {
        data = await this.$axios.post('com/appConfig', this.formData)
      }

      if (data.data.code != 200) return this.$message.error(data.data.msg)
      this.$message({ type: 'success', message: data.data.msg })
      this.getData()
    }
  },
  mounted() {
    this.getData()
  }
}
</script>

<style lang="scss" scoped>
.page {
  margin-top: 15px;
  display: flex;
  justify-content: flex-end;
}
</style>